#include "translation_next.h"

translation_next::translation_next(QObject *parent) : QObject(parent)
{

    manager = new QNetworkAccessManager(NULL);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ReplyFinished(QNetworkReply*)));
    connect(&timer,&QTimer::timeout,[=](){timer.stop();this->translation(*eng[biaoshi_chi]);});
}

void translation_next::trans_next(QString **temp, int biaoshi, int maxline)
{
    this->eng=temp;
    this->biaoshi_chi=biaoshi;
    this->maxline=maxline;

    translation(*eng[biaoshi_chi]);
}

void translation_next::translation(QString str)
{
    Translator translator;
    translator.setappid();
    translator.SetQstr(str);     //传入需要翻译内容
    QString url = translator.GetUrl();     //获取url
//    qDebug()<<url;
    //1. 创建一个请求
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    //2. 创建一个管理器

    //3. 连接请求结束信号

    //4. 发送GET请求
    manager->get(request);

    return;
}

void translation_next::slot_cunrushujuku()
{
       qDebug()<<"void to_mp4::cunrushujuku()";
        //数据库:
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          mysql = QSqlDatabase::database("qt_sql_default_connection");
        else
          mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku_engchi.db");

        if(mysql.open()==true)
        {
            QSqlQuery query;
            //
            query.exec("DROP TABLE chinese");
            query.exec("create table chinese(id int primary key, time double,chi varchar(600))");

            for(int i=0;i<this->maxline;i++)
            {
                double time=0;
                QSqlQuery query;
                query.exec(QString("select * from english where id = %1").arg(i));
                if(query.first()==true)
                {
                    time=query.value("time").toDouble();

                }
                 //qDebug()<<"id="<<i<<"time="<<time<<"chi"<<*eng[i];
                 query.exec(QString("insert into chinese(id,time,chi) values(%1,%2,'%3')").arg(i).arg(time).arg(*eng[i]) );
            }


            mysql.close();
        }


        //用完delete
        delete [] eng;

        emit signal_nexttrans_over();
        return;

}


//ASSERT: "a && i >= 0 && i < (int)a->length" in file json\qjsonarray.cpp, line 592
void translation_next::ReplyFinished(QNetworkReply *reply)
{

    QVariant status_code = reply->attribute
            (QNetworkRequest::HttpStatusCodeAttribute);
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QString result(bytes);  //转化为字符串
        QJsonObject data(QJsonDocument::fromJson(bytes).object());
        QString string = data["trans_result"].toArray()[0].toObject()["dst"].toString();
        //
        *eng[this->biaoshi_chi]=string;
        qDebug()<<"next"<<biaoshi_chi<<string;

        biaoshi_chi++;//往后移
        qDebug()<<"maxline="<<maxline;
        if(biaoshi_chi==this->maxline)//for(int i=0;i<maxline;i++)
        {
            qDebug()<<"chunrushujuku";            this->slot_cunrushujuku();
            return;
        }
        else{
            timer.start(1000);
//            this->translation(*eng[biaoshi_chi]);
            emit signal_showproject(QString("NEXT 正在翻译中...\n"+string),(double)biaoshi_chi/(double)maxline);//qDebug()<<"next"<<(biaoshi_chi)/maxline;
        }

    }
    delete reply;

    return;
}
